#!/bin/bash

cd busybox
sudo make distclean


if [ "$1" == "arm" ]
then
	ARCH=arm
	CROSS_COMPILE=arm-linux-gnueabi-
elif [ "$1" == "arm64" ]
then
	ARCH=arm64
	CROSS_COMPILE=aarch64-linux-gnu-
else
	echo "unkonw argument"
	exit 1
fi

sudo rm -rf _install _install_${ARCH}

make defconfig ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE}

sed -i "s/.*CONFIG_STATIC.*/CONFIG_STATIC=y/" .config

#make allyesconfig ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
#sed -i "s/.*CONFIG_STATIC.*/CONFIG_STATIC=y/" .config
#sed -i "s/.*CONFIG_SELINUX.*/# CONFIG_SELINUX is not set/" .config
#sed -i "s/.*CONFIG_PAM.*/# CONFIG_PAM is not set/" .config
#sed -i "s/.*CONFIG_FEATURE_SH_STANDALONE.*/# CONFIG_FEATURE_SH_STANDALONE is not set/" .config

make install ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE}
#../tools/collect-src -f "busybox" -o ../busybox_src.list

cd _install

mkdir proc sys tmp root var mnt dev lib
mkdir -p system/modules

LIB_DIR=$(dirname $(which "$CROSS_COMPILE"gcc))/../lib
cp ${LIB_DIR}/*.so* lib -d
${CROSS_COMPILE}strip lib/*


sudo mknod dev/tty1 c 4 1
sudo mknod dev/tty2 c 4 2
sudo mknod dev/tty3 c 4 3
sudo mknod dev/tty4 c 4 4
sudo mknod dev/console c 5 1
sudo mknod dev/null c 1 3
cd ../..

sudo cp tools/root_fs_patch/* busybox/_install -rd

mv busybox/_install busybox/_install_${ARCH}